<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>AES/DES加密解密工具</title>
    <link rel="stylesheet" href="../../bootstrap/bootstrap.min.css">
</head>

<body>
    <div class="container mt-5">
        <h1 class="mb-4">AES/DES加密解密工具</h1>
        <form>
            <div class="form-group mb-3">
                <label for="input">输入要加密/解密的文本：</label>
                <textarea class="form-control" id="input" rows="3"></textarea>
            </div>
            <div class="form-group mb-3">
                <label for="algorithm">选择加密算法：</label>
                <select class="form-control" id="algorithm">
                    <option value="AES">AES</option>
                    <option value="DES">DES</option>
                    <option value="TripleDES">TripleDES</option>
                </select>
            </div>
            <div class="form-group mb-3">
                <label for="key">输入秘钥：</label>
                <input type="text" class="form-control" id="key" placeholder="请输入秘钥">
            </div>
            <button type="button" class="btn btn-primary" onclick="encrypt()">加密</button>
            <button type="button" class="btn btn-danger" onclick="decrypt()">解密</button>
        </form>
        <div class="mt-4">
            <p>输出结果：</p>
            <div id="result"></div>
        </div>
    </div>
    <script src="../../js/crypto-js.min.js"></script>
    <script>
        function encrypt() {
            const input = document.getElementById("input").value;
            const key = document.getElementById("key").value;
            const algorithm = document.getElementById("algorithm").value;

            if (input === "" || key === "") {
                alert("文本和秘钥都不能为空！");
                return;
            }

            let ciphertext;

            switch (algorithm) {
                case "AES":
                    ciphertext = CryptoJS.AES.encrypt(input, key).toString();
                    break;
                case "DES":
                    ciphertext = CryptoJS.DES.encrypt(input, key).toString();
                    break;
                case "TripleDES":
                    ciphertext = CryptoJS.TripleDES.encrypt(input, key).toString();
                    break;
                default:
                    alert("请选择一个加密算法！");
                    return;
            }

            document.getElementById("result").innerHTML = ciphertext;
        }


        function decrypt() {
            const input = document.getElementById("input").value;
            const key = document.getElementById("key").value;
            const algorithm = document.getElementById("algorithm").value;

            if (input === "" || key === "") {
                alert("文本和秘钥都不能为空！");
                return;
            }

            try {
                let bytes;
                switch (algorithm) {
                    case "AES":
                        bytes = CryptoJS.AES.decrypt(input, key);
                        break;
                    case "DES":
                        bytes = CryptoJS.DES.decrypt(input, key);
                        break;
                    case "TripleDES":
                        bytes = CryptoJS.TripleDES.decrypt(input, key);
                        break;
                    default:
                        alert("请选择一个加密算法！");
                        return;
                }
                const plaintext = bytes.toString(CryptoJS.enc.Utf8);
                document.getElementById("result").innerHTML = plaintext;
            } catch (error) {
                alert("解密失败，请检查秘钥是否正确！");
            }
        }
    </script>
</body>

</html>